
#include "config/IncludeParts.h"

#if USE_XML

#include "XmlDoc.h"
#include "config/Platform.h"
#include "tools/file/File.h"
#include "tools/StringManager.h"
#include "XmlNode.h"

using namespace unixml;

XmlDoc::XmlDoc(const char* name):
m_code(0)
{
    unigame::File f(name, unigame::E_RESOURCE_FT);
    f.read();
    int32 len = unigame::StringManager::utf8String(f.getData(), f.getSize(), 0) + 1;
    m_code = uni_new uint16[len];
    unigame::StringManager::utf8String(f.getData(), f.getSize(), m_code);
    f.close();
    init();
    uni_delete_array(m_code);
}

XmlDoc::~XmlDoc()
{
    uni_delete_array(m_code);
    for(int32 i = 0, size = m_nodeList.size(); i < size; ++i)
    {
        delete m_nodeList[i];
    }
}

void XmlDoc::init()
{
    int32 pointer = 0;
    while(pointer != -1)
    {
        pointer = unigame::StringManager::uniFindChar('<', m_code, pointer);
        if(pointer != -1)
        {
            XmlNode* node = uni_new XmlNode(m_code, pointer);
            pointer = node->getEnd();
            if(node->isNode())
            {
                m_nodeList.push_back(node);
            }
            else
            {
                delete node;
            }
        }
    }
}

#endif
